package com.example.myui.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.text.TextUtilsCompat;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.bumptech.glide.load.data.InputStreamRewinder;
import com.example.myui.R;
import com.example.myui.utils.DensityUtil;

import java.util.Map;


/**
 * Created by Administrator on 2018/1/18/018.
 * 自定义控件
 */

public class MyCircleTest extends ProgressBar {

    private static final int DEFAULT_TEXT_SIZE = 30;
    private static final int DEFAULT_TEXT_COLOR = R.color.red;
    private static final int DEFAULT_REACH_COLOR = R.color.yellow;
    private static final int DEFAULT_UNREACH_COLOR = R.color.black;
    private static final int DEFAULT_UNREACH_HEIGHT = 5;
    private static final int DEFAULT_REACH_HEIGHT = 5;
    private static final int DEFAULT_OFFSET = 10;

    private int mTextSize = DensityUtil.dp2px(DEFAULT_TEXT_SIZE);
    private int mTextColor = DEFAULT_TEXT_COLOR;
    private int mReachColor = DEFAULT_REACH_COLOR;
    private int mUnReachColor = DEFAULT_UNREACH_COLOR;
    private int mReachHeight = DensityUtil.dp2px(DEFAULT_REACH_HEIGHT);
    private int mUnReachHeight = DensityUtil.dp2px(DEFAULT_UNREACH_HEIGHT);
    private int mOffset = DensityUtil.dp2px(DEFAULT_OFFSET);

    private int realWidth;
    private int center;

    private Paint mPaint = new Paint();
    private int widthSize;
    private int heightSize;
    private int min;
    private RectF rectF = null;

    public MyCircleTest(Context context) {
        this(context, null);
    }

    public MyCircleTest(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCircleTest(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        @SuppressLint("CustomViewStyleable")
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCirclrTest, defStyleAttr, 0);
        mTextSize = (int) ta.getDimension(R.styleable.MyCirclrTest_progress_text_size_1, DEFAULT_TEXT_SIZE);
        mTextColor = ta.getInt(R.styleable.MyCirclrTest_progress_text_color_1, DEFAULT_TEXT_COLOR);
        mReachColor = ta.getInt(R.styleable.MyCirclrTest_progress_reach_color_1, DEFAULT_TEXT_COLOR);
        mUnReachColor = ta.getInt(R.styleable.MyCirclrTest_progress_unreach_color_1, DEFAULT_UNREACH_COLOR);
        mPaint.setTextSize(mTextSize);
//        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        rectF = new RectF(100, 100, 200, 200);
        ta.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int height = measureHeight(heightMode, heightSize);
        setMeasuredDimension(widthSize, height);
        realWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();


//        min = Math.min(widthSize, heightSize);
//        float paintWidth = mPaint.getStrokeWidth() / 2;
//        setMeasuredDimension(min, min);
    }

    private int measureHeight(int heightMode, int heightSize) {
        int result = 0;

        if (heightMode == MeasureSpec.EXACTLY) {
            result = heightSize;
        } else {
            int textHeight = (int) (mPaint.descent() - mPaint.ascent());
            result = getPaddingTop() + getPaddingBottom() -
                    Math.max(Math.max(mReachHeight, mUnReachHeight), Math.abs(textHeight));
            if (heightMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, heightSize);
            }
        }
        return result;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.save();
//        canvas.drawColor(Color.parseColor("#44ff0000"));
//        canvas.drawLine(0, 0, min, 0, mPaint);
//        canvas.drawLine(min, 0, min, min, mPaint);
//        canvas.drawLine(min, min, 0, min, mPaint);
//        canvas.drawLine(0, min, 0, 0, mPaint);
//
//        canvas.translate(min / 2, min / 2);
//        canvas.drawLine(0, 0, min / 2, 0, mPaint);
//        canvas.drawRect(rectF, mPaint);
//        canvas.restore();
//        canvas.drawArc(rectF, 0, 190, false, mPaint);
        canvas.save();
        canvas.translate(getPaddingLeft(), getHeight() / 2);

        boolean noNeedReach = false;
        String text = getProgress() + "%";
        float textWidth = mPaint.measureText(text);
        float radio = getProgress() * 1.0f / getMax();
        float progressX = radio * realWidth;
        if (progressX + textWidth > realWidth) {
            progressX = realWidth - textWidth;
            noNeedReach = true;
        }

        float endX = progressX - mOffset / 2;
        if (endX > 0) {
            mPaint.setColor(mReachColor);
            mPaint.setStrokeWidth(mReachHeight);
            canvas.drawLine(0, 0, endX, 0, mPaint);
        }

        mPaint.setColor(mTextColor);
        int y = (int) -(mPaint.descent() + mPaint.ascent() / 2);
        canvas.drawText(text, progressX, y, mPaint);

        if (!noNeedReach) {
            float start = progressX + mOffset / 2 + textWidth;
            mPaint.setColor(mUnReachColor);
            mPaint.setStrokeWidth(mUnReachHeight);
            canvas.drawLine(start, 0, realWidth, 0, mPaint);
        }


        canvas.restore();

    }
}
